<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>管理员添加</title>
    <link href="__STATIC__/admin/js/bootstrap/css/bootstrap.css" rel="stylesheet">
    <link href="__STATIC__/admin/css/AdminLTE.css" rel="stylesheet" type="text/css" />
    <link href="__STATIC__/admin/css/upload.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/admin/css/webuploader.css" />
</head>
<body class="wst-page">
<form method="post" id="upload_form" enctype="multipart/form-data">
    <input type='hidden' id='id' value=''/>
    <table class="table table-hover table-striped table-bordered wst-form">
        <tr>
            <th width='120' align='right'>昵称：</th>
            <td><input type="text" name="staff_name" id="staff_name" value=""></td>
            <td rowspan='6'>
                <div>
                    <img class="fimg" id="preview_Filedata" src=""><br/>
                </div>
            </td>
        </tr>
        <tr>
            <th align='right'>登录用户名：</th>
            <td>
                <input type='text' id='login_name' class="form-control wst-ipt" value=''  maxLength='20'/>
            </td>
        </tr>
        <tr>
            <th align='right'>密码：</th>
            <td>
                <input type='password' id='login_pwd' class="form-control wst-ipt" value='' maxLength='20'/>
            </td>
        </tr>
        <tr style="height:80px;">
            <th align='right'>用户头像：</th>
            <td colspan='2'>
                <div>
                    <div id="filePicker" style='margin-left:0px;float:left'>上传头像:<input type="file" name="photo_Upload" value="" onchange="fileImg()"></div>
                    <div style='margin-left:5px;float:left'>图片大小:150 x 120 (px)，格式为 gif, jpg, jpeg, png</div>
                    <input id="staff_photo" name="staff_photo" class="text wstipt" tabindex="3" autocomplete="off" type="hidden" value=""/>
                </div>
            </td>
        </tr>
        <tr>
        <tr>
            <td colspan='3' style='padding-left:250px;'>
                <button type="button" class="submit btn btn-success">保&nbsp;存</button>
                <button type="button" class="btn btn-primary">返&nbsp;回</button>
            </td>
        </tr>
    </table>
</form>
</body>
<script>
    //上传图片jquery
    function fileImg() {
        var upload_form = $("#upload_form")[0];
        var data_obj = new FormData(upload_form);
        layer.load();
        setTimeout(function(){
            layer.closeAll('loading');
        }, 1000);
        $.ajax({
            url: "{:url('admin/Courses/uploadImg')}",
            method: "POST",
            data: data_obj,
            contentType: false,//文件上传必须
            processData: false,//文件上传必须
            success: function (data) {
                var jost = JSON.parse( data );
                if(jost != 1){
                    layer.msg('上传成功', {icon: 1,time: 2000});
                    var imgUrl = "__ROOT__/uploads/photo/"+jost;
                    $(".fimg").attr('src',imgUrl);
                    $(".wstipt").attr('value',imgUrl);
                }else{
                    layer.msg('上传失败', {icon: 2,time: 2000});

                }
            }
        });
    }

    $(document).ready(function(){
        $(".submit").click(function () {
            var data = {};
            data.staff_name = $("#staff_name").val();
            data.login_name = $("#login_name").val();
            data.login_pwd = $("#login_pwd").val();
            data.staff_photo = $("#staff_photo").val();
            if( data.staff_name == ""){
                layer.msg('昵称不能为空', {icon: 2});
                return;
            }
            if( data.login_name == ""){
                layer.msg('登录名不能为空', {icon: 2});
                return;
            }
            if( data.login_pwd == ""){
                layer.msg('密码不能为空', {icon: 2});
                return;
            }
            if( data.userphoto == ""){
                layer.msg('请上传头像', {icon: 2});
                return;
            }
            $.post("{:url('admin/staff/staffInset')}",data,function (data) {
                console.log(data);
                if(data > 0){
                    layer.msg('添加成功', {icon: 1,time: 2000});
                    setTimeout(function(){
                        window.location.href="{:url('admin/staff/staffList')}"
                    },2000);
                }else {
                    layer.msg('添加失败', {icon: 2});
                }
            })
        })
    })


</script>
<script src="__STATIC__/admin/js/jquery.min.js"></script>
<script src="__STATIC__/admin/js/bootstrap/css/bootstrap.css"></script>
<script src="__STATIC__/admin/js/common.js"></script>
<script src="__STATIC__/admin/js/plugins.js"></script>
<script src="__STATIC__/admin/js/formValidator-4.1.3.js"></script>
<script src="__STATIC__/admin/js/layer/layer.js"></script>
</html>